package com.example.blue.musicplay.http;

import android.net.ParseException;

import com.example.blue.musicplay.basic.OnlineMusicInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by blue on 2016/9/20.
 */

public class GetNewMusic {
    /*新歌uri*/
    private static final String newMusic_str = "http://tingapi.ting.baidu.com/v1/restserver/ting?format=json&method=baidu.ting.billboard.billList&type=1&size=10&offset=0";
    private static HttpURLConnection connection = null;
    private static List<OnlineMusicInfo> list_music_info = new ArrayList<>();

    public List<OnlineMusicInfo> getInfo() {
        try {
            URL url = new URL(newMusic_str);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            InputStreamReader reader = new InputStreamReader(
                    connection.getInputStream(), "UTF-8");
            BufferedReader breader = new BufferedReader(reader);
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = breader.readLine()) != null) {
                builder.append(line);
            }
            ParseJson(builder.toString());
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (JSONException e) {

            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            connection.disconnect();
            connection = null;
        }
        return list_music_info;
    }

    /*
      解析json数据
     */
    public void ParseJson(String jsonString) throws JSONException,
            ParseException {
        JSONObject dataJson = new JSONObject(jsonString);
        JSONArray data = dataJson.getJSONArray("song_list");
        OnlineMusicInfo music_info = null;
        for (int i = 0; i < data.length(); i++) {
            music_info = new OnlineMusicInfo();
            JSONObject info = data.getJSONObject(i);
            String song_id = info.getString("song_id");
            music_info.setSong_link(SearchSong.getInstance().search(song_id,"songLink"));
            music_info.setSong_name(SearchSong.getInstance().search(song_id, "songName"));
            music_info.setSong_id(song_id);
            String pic_big = info.getString("pic_big");
            music_info.setPic_link(pic_big);
            String lrclink = info.getString("lrclink");
            music_info.setLrc_link(lrclink);
            String artist_name = info.getString("artist_name");
            music_info.setArtist_name(artist_name);
            list_music_info.add(music_info);
        }
    }
}
